/*
 * 文件名: PerWeeksPerMothsLinearLayout.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2011-12-30
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import app.log.APPLog;
import app.utils.CommonUtils;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2011-12-30
 * @version [Android PABank C01, @2011-12-30]
 * @description 每周每月的自定义LinearLayout
 */
public class PerWeeksPerMothsLinearLayout extends LinearLayout implements
		OnWheelChangedListener {

	/**
	 * 返回的星期结果 星期日为0 其余对应数字
	 */
	private int mWeek;
	/**
	 * 返回的天 结果 1-28 周末为32
	 */
	private int mDay;
	/**
	 * 当前周
	 */
	private int curWeek;
	/**
	 * 当前是上午还是下午
	 */
	private int curAmpm;
	/**
	 * 当前天
	 */
	int curDay;
	/**
	 * 当前选择是每周为1，每月为2
	 */
	private int curSelect = 1;
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 滚轮View 每周，每月
	 */
	private WheelView per;

	/**
	 * 滚轮View 天数
	 */
	private WheelView day;
	/**
	 * 滚轮View 上午下午
	 */
	private WheelView ampm;
	/**
	 * 滚轮View 周
	 */
	private WheelView week;
	/**
	 * 滚轮View 日历
	 */
	private Calendar calendar = CommonUtils.getCurrentTimeByCalendarAmpm();
	/**
	 * inflater
	 */
	private LayoutInflater mInflater;
	/**
	 * 是否执行过onLayout
	 */
	private boolean isOnLayout = false;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public PerWeeksPerMothsLinearLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public PerWeeksPerMothsLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化操作
	 */
	private void init() {
		mInflater = LayoutInflater.from(getContext());
		View root = mInflater.inflate(R.layout.app_widget_perweek_permonth_layout, null);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		root.setLayoutParams(lp);
		addView(root);

		per = (WheelView) findViewById(R.id.m_per);
		ampm = (WheelView) findViewById(R.id.m_ampm);
		day = (WheelView) findViewById(R.id.m_day);
		week = (WheelView) findViewById(R.id.m_week);

		CalendarDialogCommon.addChangeListener(this, per, week, day, ampm);
		per.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.PER_W_M, 0));
		per.setCurrentItem(0);

		curWeek = calendar.get(Calendar.DAY_OF_WEEK);
		week.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.WEEKS, curWeek - 1));
		week.setCurrentItem(curWeek - 1);

		curDay = calendar.get(Calendar.DAY_OF_MONTH);
		int index = curDay - 1;
		if (curDay > 28) {
			index = CalendarDialogCommon.DAYS.length - 1;
			curDay = 32;
		}
		mDay = curDay;
		day.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.DAYS, index));
		day.setCurrentItem(index);

		curAmpm = calendar.get(Calendar.AM_PM);

		ampm.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.AM_PM, curAmpm));
		ampm.setCurrentItem(curAmpm);

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		/**
		 * 监听回调
		 */
		if (!isOnLayout) {
			onWeekOrMonthChangeListener.onWeekOrMonthChange(curSelect, mWeek,
					CalendarDialogCommon.convertZeroString(curAmpm));
			isOnLayout = true;
		}

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2011-12-30]
	 */
	public interface OnWeekOrMonthChangeListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述] 每周每月改变时回调方法
		 * @param isMonthOrWeek 月，周
		 * @param result 结果
		 * @param ampm 上午下午
		 *
		 */
		void onWeekOrMonthChange(int isMonthOrWeek, int result, String ampm);

	}

	/**
	 * 回调监听实例
	 */
	private OnWeekOrMonthChangeListener onWeekOrMonthChangeListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @return OnWeekOrMonthChangeListener
	 */
	public OnWeekOrMonthChangeListener getOnWeekOrMonthChangeListener() {
		return onWeekOrMonthChangeListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onWeekOrMonthChangeListener
	 *            回调监听实例
	 */
	public void setOnWeekOrMonthChangeListener(
			OnWeekOrMonthChangeListener onWeekOrMonthChangeListener) {
		this.onWeekOrMonthChangeListener = onWeekOrMonthChangeListener;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		// TODO Auto-generated method stub

		switch (wheel.getId()) {
		case R.id.m_per:
			switch (newValue) {
			case 0:
				day.setVisibility(View.GONE);
				week.setVisibility(View.VISIBLE);

				curSelect = 1;

				break;
			case 1:
				day.setVisibility(View.VISIBLE);
				week.setVisibility(View.GONE);

				curSelect = 2;

				break;

			default:
				break;
			}

			break;
		case R.id.m_week:

			mWeek = newValue;

			break;

		case R.id.m_day:
			if (newValue > 27) {
				mDay = 32;
			} else {

				mDay = 1 + newValue;
			}
			break;
		case R.id.m_ampm:
			curAmpm = newValue;
			break;
		default:
			break;
		}

		if (onWeekOrMonthChangeListener != null) {
			switch (curSelect) {
			case 1:
				APPLog.i("zbkc", curSelect + "|" + mWeek + "|" + curAmpm);
				onWeekOrMonthChangeListener.onWeekOrMonthChange(curSelect,
						mWeek, CalendarDialogCommon.convertZeroString(curAmpm));
				break;
			case 2:
				APPLog.i("zbkc", curSelect + "|" + mDay + "|" + curAmpm);
				onWeekOrMonthChangeListener.onWeekOrMonthChange(curSelect,
						mDay, CalendarDialogCommon.convertZeroString(curAmpm));
				break;
			default:
				break;
			}

		}

	}

}
